﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PropertyManagementSystemAPI.Models;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 车位管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ParkingSpaceInfoController : ControllerBase
    {
        private readonly IFreeSql db;

        public ParkingSpaceInfoController(IFreeSql _db)
        {
            this.db = _db;
        }


        /// <summary>
        /// 查询停车位信息
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">页条数</param>
        /// <param name="carnumber">车位编号</param>
        /// <param name="stratTime">开始时间</param>
        /// <param name="endtime">结束时间</param>
        /// <returns></returns>
        /// //,DateTime stratTime,DateTime endtime
        [HttpGet]

        public IActionResult getParkingSpaceInfo(int page, int limit, int state, string? carnumber)
        {
            bool stateRes = state == 1 ? true : false;
            List<Parkingspaceinfo> list = db.Select<Parkingspaceinfo>()
                .Where(a => ((string.IsNullOrEmpty(carnumber)) || (a.Pnumber.Contains(carnumber))) && ((state == 2) || (a.Pstate == stateRes)) && a.Pdelete == false)
                .Count(out var total)
                .Page(page, limit)
                .ToList();

            var json = new
            {
                code = 200,
                data = list,
                total = total
            };
            return Ok(json);
        }

        /// <summary>
        /// 添加车位信息
        /// </summary>
        /// <param name="parkingspaceinfo">提交的信息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInfo([FromBody] Parkingspaceinfo parkingspaceinfo)
        {
            try
            {
                parkingspaceinfo.Createtime = DateTime.Now;
                int res = db.Insert(parkingspaceinfo).ExecuteAffrows();
                return Ok(res > 0);
            }
            catch (Exception)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 编辑车位信息
        /// </summary>
        /// <param name="info">提交的信息</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult EditInfo([FromBody] Parkingspaceinfo info)
        {
            try
            {
                int res = db.Update<Parkingspaceinfo>(info.Pid)
                    .Set(a => a.Pname, info.Pname)
                    .Set(a => a.Pnumber, info.Pnumber)
                    .Set(a => a.Pstate, info.Pstate)
                    .ExecuteAffrows();
                return Ok(res > 0);
            }
            catch (Exception)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 删除车位信息
        /// </summary>
        /// <param name="id">车位id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Delete(int id)
        {
            bool boolres = false;
            int res = 0;
            string msg = string.Empty;
            try
            {
                Parkingspaceinfo parkingspaceinfo = db.Select<Parkingspaceinfo>(id).First();
                if (parkingspaceinfo.Pstate == false)
                {
                    res = db.Update<Parkingspaceinfo>(id).Set(a => a.Pdelete == true).ExecuteAffrows();
                    boolres = res > 0;
                    msg = boolres ? "删除成功" : "删除失败";
                }
                else
                {
                    boolres = false;
                    msg = "该车位在使用中，不可删除!";
                }


            }
            catch (Exception)
            {
                boolres = false;
                msg = "删除出现异常，请联系管理员";
            }

            var json = new
            {
                res = boolres,
                msg = msg
            };
            return Ok(json);
        }

        /// <summary>
        /// 获取车位信息绑定下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("getParkingInfoBindSelect")]
        public IActionResult getParkingInfoBindSelect()
        {
            var list = db.Select<Parkingspaceinfo>()
                .Where(a => a.Pdelete == false)
                .ToList(a => new 
                { 
                    a.Pid, 
                    Pname = a.Pstate == true ? a.Pname + " （该车位使用中）" : a.Pname, 
                    a.Pstate 
                });
            return Ok(list);
        }
    }
}
